package org.kpli.jak.net.first;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;

public class InfoClient extends SocketException {

	private static final long serialVersionUID = 1L;
	private final int INFO_PORT = 50010;
	private final String targetHost = "localhost";
	private final String QUIT = "QUIT";

	public InfoClient() {
		try {
			BufferedReader inFromUser = new BufferedReader(
					new InputStreamReader(System.in));
			Socket clientSocket = new Socket(targetHost, INFO_PORT);
			DataOutputStream outToServer = new DataOutputStream(
					clientSocket.getOutputStream());
			BufferedReader inFromServer = new BufferedReader(
					new InputStreamReader(clientSocket.getInputStream()));
			System.out.println(inFromServer.readLine());
			System.out.println("");
			boolean isQuit = false;

			while (!isQuit) {
				System.out.print("Perintah Anda : ");
				String cmd = inFromUser.readLine();
				cmd = cmd.toUpperCase();
				if (cmd.equals(QUIT)) {
					isQuit = true;
				}
				outToServer.writeBytes(cmd + "\n");
				String result = inFromServer.readLine();
				System.out.println("Dari Server: " + result);
			}
			outToServer.close();
			inFromServer.close();
			clientSocket.close();
		} catch (IOException ioe) {
			System.out.println("Error:" + ioe);
		} catch (Exception e) {
			System.out.println("Error:" + e);
		}
	}

	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		new InfoClient();
	}
}
